;'use strict';
(function () {
    function isValidEmail(email) {
        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        return emailRegex.test(email);
    }

    const after_n_seconds = gettext('%(seconds)ss 后重发');
    $('#send-code-btn').click(function (e) {
        e.preventDefault(); // 兼容标准浏览器

        const emailInput = document.querySelector('input[name="email"]');
        const btn = document.getElementById('send-code-btn');
        const email = emailInput.value.trim();

        const params = new URLSearchParams(window.location.search);
        const external_user_id = params.get('external_user_id');

        let origin = window.location.origin;
        if (!origin.endsWith('/'))
            origin = origin + '/'

        const email_code_url = origin + 'api/auth_provider/email/verification?' + $.param({
            'external_user_id': external_user_id,
            'email': email
        });

        if (!isValidEmail(email)) {
            Swal.fire({
                title: gettext("请输入一个有效的邮箱"),
                icon: "warning",
                timer: 2000,
                draggable: true
            });
            return;
        }

        // 禁用按钮并开始倒计时
        let countdown = 60;
        btn.disabled = true;
        btn.textContent = interpolate(after_n_seconds, {seconds: countdown}, true);

        const timer = setInterval(() => {
            countdown--;
            if (countdown > 0) {
                btn.textContent = interpolate(after_n_seconds, {seconds: countdown}, true);
            } else {
                clearInterval(timer);
                btn.disabled = false;
                btn.textContent = gettext('重新发送验证码');
            }
        }, 1000);

        const htmlLang = document.documentElement.lang;
        let acceptlanguage = 'zh-hans';
        if (htmlLang === 'en') {
            acceptlanguage = 'en';
        }

        // 发送请求
        $.ajax({
            url: email_code_url,
            type: 'post',
            contentType: 'application/json',
            headers: {
                'Accept-Language': acceptlanguage,
            },
            success: function (data, status, xhr) {
                if (xhr.status === 200) {
                    Swal.fire({
                        position: "top-end",
                        icon: "success",
                        title: gettext("邮件已发送"),
                        showConfirmButton: false,
                        timer: 1500
                    });
                } else {
                    clearInterval(timer);
                    btn.disabled = false;
                }
            },
            error: function (xhr) {
                let msg = gettext('发送验证码失败。');
                clearInterval(timer);
                btn.disabled = false;
                btn.textContent = gettext('发送验证码');

                try {
                    msg = msg + xhr.responseJSON.message
                } catch (e) {
                }
                Swal.fire({
                    title: msg,
                    icon: "error",
                    draggable: true
                });
            },
        })
    })
})();
